package com.sparrow.socket.tcp.init;

import com.sparrow.socket.tcp.codec.RequestDecoder;
import com.sparrow.socket.tcp.codec.ResponseEncoder;
import com.sparrow.socket.tcp.codec.VerifyDecoder;
import com.sparrow.socket.tcp.handler.*;
import com.sparrow.socket.tcp.codec.LengthFieldDecoder;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;

public abstract class TcpServerInitializer<C> extends ChannelInitializer {
    @Override
    protected void initChannel(Channel channel) throws Exception {
        ChannelPipeline pipeline = channel.pipeline();
        pipeline.addLast(new SimpleLoggingHandler(LogLevel.INFO));
        pipeline.addLast(new ServerStateHandler());
        pipeline.addLast(new IpLimitHandler());
        pipeline.addLast(new ResponseEncoder());
        pipeline.addLast(new IdleStateHandler(60, 0, 0));
        pipeline.addLast(new TcpIdleStateHandler());
        pipeline.addLast(new LengthFieldDecoder());
        pipeline.addLast(new FlowFireHandler());
        pipeline.addLast(new VerifyDecoder());
        pipeline.addLast(new RequestDecoder());
        initChannel(pipeline, channel);
    }

    protected abstract void initChannel(ChannelPipeline pipeline, Channel channel);
}
